package com.lv.dao;

import com.lv.bean.Token;
import com.lv.utils.BaseUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.util.ArrayList;
import java.util.List;

public class TokenDaoImpl implements TokenDao {
    @Override
    public Token selectTokenByCorpId(String corp_id) {
        //创建QueryRunner
        QueryRunner qr=BaseUtils.getQueryRunner();
        String sql="select * from dd_token where corp_id=? ";
        try {
            //使用QueryRunner的强大之处在于此处。
            return qr.query(sql,new BeanHandler<Token>(Token.class), corp_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean updateToken(Token token) {
        String sql="update dd_token set access_token=?,begin_time=?,ticket=? where corp_id=?;";
        List<Object> list=new ArrayList<>();
        if(token!=null){
            list.add(token.getAccess_token());
            list.add(token.getBegin_time());
            list.add(token.getTicket());
            //根据编号修改信息
            list.add(token.getCorp_id());
        }

        try {
            return BaseUtils.addUpdateDelete(sql, list.toArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean insertToken(Token token) {
        String sql = "insert into dd_token (corp_id,access_token,begin_time,ticket) values(?,?,?,?);";

        List<Object> list = new ArrayList<>();
        if (token != null) {
            list.add(token.getCorp_id());
            list.add(token.getAccess_token());
            list.add(token.getBegin_time());
            list.add(token.getTicket());
        }
        try {
            return BaseUtils.addUpdateDelete(sql, list.toArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
